package com.developer.platform.application.query.assembler;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.RoleDto;
import com.developer.platform.application.query.dto.RoleListDto;
import com.developer.platform.application.query.dto.UserRoleDto;
import com.developer.platform.domain.Role;
import com.developer.platform.domain.UserRole;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class RoleAssembler extends BaseAssembler{


    public ResponseModel<RoleListDto> toRoleListDto(IPage<Role> page){
        List<RoleListDto> collect = page.getRecords().stream().map(role -> {
            RoleListDto roleListDto = new RoleListDto();
            BeanUtil.copyProperties(role, roleListDto);
            return roleListDto;
        }).collect(Collectors.toList());
        this.userName(collect);
        return  new ResponseModel<>(collect, (int)page.getTotal());
    }


    public List<RoleDto> toRoleDto(List<Role> list){
        return list.stream().map(role -> {
            RoleDto roleDto = new RoleDto();
            BeanUtil.copyProperties(role, roleDto);
            return roleDto;
        }).collect(Collectors.toList());
    }


    public UserRoleDto toUserRoleDto(List<UserRole> list, List<RoleDto> roleDtoList){
        return new UserRoleDto(list.stream().map(UserRole::getRoleId).collect(Collectors.toList()),
                roleDtoList);
    }
}
